<template>
  <div class="body">
    <el-button @click="add_warehouse" style="margin-left: 15px"
      >新增供应商</el-button
    >
    <el-table :data="productList" border>
      <el-table-column prop="code" label="供应商编号"> </el-table-column>
      <el-table-column prop="name" label="供应商名称"> </el-table-column>
      <el-table-column prop="link_man" label="负责人"> </el-table-column>
      <el-table-column prop="bank_name" label="开户银行名称"> </el-table-column>
      <el-table-column prop="bank_account" label="银行账号"> </el-table-column>
      <el-table-column prop="address" label="联系地址"> </el-table-column>    
      <el-table-column label="操作" width="260" fixed="right">
        <template slot-scope="scope">
          <el-button
            @click="add_warehouse(scope.row)"
            type="text"
            icon="el-icon-edit"
            >编辑</el-button
          >
          <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            icon="el-icon-info"
            icon-color="red"
            title="是否删除这条数据？"
            @confirm="del_product(scope.row)"
          >
            <el-button slot="reference" type="text" icon="el-icon-delete">
              删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        @prev-click="prev"
        @next-click="next"
        :current-page.sync="pagination.page"
        :page-size="pagination.per_page"
        layout="total, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>

    <el-dialog
      :title="title"
      :visible.sync="isProduct"
      width="40%"
      :close-on-click-modal="false"
      @close="close_dialog"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="110px"
        class="demo-ruleForm"
      >
        <el-row>
          <el-col :span="11">
            <el-form-item label="供应商编号" prop="code">
              <el-input v-model="ruleForm.code"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="供应商名称" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="类型" prop="type">
              <el-select
                v-model="ruleForm.type"
                placeholder="请选择类型"
                style="width: 100%"
              >
                <el-option
                  v-for="(item, index) in typelist"
                  :key="index"
                  :label="item.name"
                  :value="item.type"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="联系人" prop="link_man">
              <el-input v-model="ruleForm.link_man"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="联系电话" prop="tel">
              <el-input v-model="ruleForm.tel"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="联系地址" prop="address">
              <el-input v-model="ruleForm.address"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="开户银行名称" prop="bank_name">
              <el-input v-model="ruleForm.bank_name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="银行账号" prop="bank_account">
              <el-input v-model="ruleForm.bank_account"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close_dialog">取 消</el-button>
        <el-button type="primary" @click="submitaddhuojia('ruleForm')"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getSupplierList,createSupplier,delSupplier } from "@/api/product/index";
export default {
  data() {
    return {
      typelist: [
        {
          type: 1,
          name: "厂家",
        },
        {
          type: 2,
          name: "代理商",
        },
        {
          type: 3,
          name: "个人",
        },
      ],
      huojiatitle: "",
      title: "",
      isProduct: false,
      ishuojia: false,
      productList: [],
      pagination: {
        per_page: 10,
        page: 1,
      },
      total: 0,
      warehouse_name: "",
      id: "",
      ruleForm: {
        id: "",
        code: "",
        name: "",
        type: "",
        link_man: "",
        tel: "",
        address: "",
        bank_name: "",
        bank_account: "",
      },
      rules: {
        code:[
          { required: true, message: '请输入供应商编号', trigger: 'blur' }
        ],
        name:[
          { required: true, message: '请输入供应商名称', trigger: 'blur' }
        ],
        type:[
          { required: true, message: '请选择类型', trigger: 'blur' }
        ],
        link_man:[
          { required: true, message: '请输入联系人', trigger: 'blur' }
        ],
        tel:[
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        address:[
          { required: true, message: '请输入供应商地址', trigger: 'blur' }
        ],
        bank_name:[
          { required: true, message: '请输入供应商开户行', trigger: 'blur' }
        ],
        bank_account:[
          { required: true, message: '请输入银行卡号', trigger: 'blur' }
        ]
      },
      isExpandAll: false,
    };
  },
  mounted() {
    this.getlist();
  },
  methods: {
    del_product(row){
        delSupplier({id:row.id,is_del:1}).then(res=>{
          if(res.code==200){
            this.$message({
              type: "success",
              message: "删除成功",
            });
           this.addhuojia={
              goods_shelf_name:'',
              goods_shelf_id:'',
              warehouse_id:''
            },
            this.getlist();
          }else{
            this.$message({
              type: "error",
              message: res.msg,
            });
          }
        })
    },
    close_dialog(){
        this.isProduct = false;
        this.ruleForm={
        id: "",
        code: "",
        name: "",
        type: "",
        link_man: "",
        tel: "",
        address: "",
        bank_name: "",
        bank_account: "",
      }
    },
    submitaddhuojia(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
            createSupplier(this.ruleForm).then(res => {
            if (res.code == 200) {
              this.isProduct = false
              this.$message.success('提交成功')
              this.getlist()
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    add_warehouse(row) {
        if(row.id){
            this.ruleForm=row
            this.title = "编辑供应商";
        }else{
            this.title = "新增供应商";
        }
        this.isProduct = true;
    },
    getlist() {
      getSupplierList(...this.pagination,...this.ser).then((res) => {
        if (res.code == 200) {
          this.productList = res.data.data;
          this.total = res.data.total;
        }
      });
    },
    handleSizeChange(val) {
      console.log(val);
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagination.page = val;
      this.getlist();
    },
    prev() {
      this.pagination.page = this.pagination.page - 1;
      this.getlist();
    },
    next() {
      console.log("ss");
      this.pagination.page = this.pagination.page + 1;
      this.getlist();
    },
  },
};
</script>

<style scoped></style>
